package com.fch.sdlibrary.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @program: SdLibrary
 * @description: 登录实体类
 * @CreatedBy: fch
 * @create: 2022-04-22 22:37
 **/
@Data
public class User implements UserDetails {
    private String userId;
    private String userName;
    private String userPasw;
    private String userPhone;
    private String userEmil;
    private Integer userCando;
    private String userRole;
    private Integer userEnabled;
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss", timezone = "GMT+8")
    private Date created;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_" + this.userRole));
        return authorities;
    }

    @Override
    public String getPassword() {
        return userPasw;
    }

    @Override
    public String getUsername() {
        return userId;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return userEnabled == 1;
    }
}
